package com.example.javaweb;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取用户提交的数据
        String username = req.getParameter("username");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        String email = req.getParameter("email");
        //2、封装
        User user = new User(username,password1,password2,email);
        //3、验证
        boolean yanzheng = user.yanzheng();
        if(!yanzheng){
            //3.1验证失败，跳转到注册页面，并显示错误信息
            req.setAttribute("user",user);
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }else {
            User user1 = DBUser.getUser(username);
            if(user1!=null){
                req.setAttribute("errMsg","该用户已注册");
                req.getRequestDispatcher("register.jsp").forward(req,resp);
            }else{
                boolean insert = DBUser.insert(user);
                if(insert==true){
                    req.setAttribute("user",user);
                    req.getRequestDispatcher("success.jsp").forward(req,resp);
                }
            }
        }
        //校验用户是否注册



        //跳转成功页面


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
